package com.sgb.kjwl.view.ui.activity.self.mine

import android.content.Context
import android.content.Intent
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityFastMenuBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity
import com.swgk.core.util.ACAdminDialogUtils
import com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity
import com.sgb.kjwl.view.ui.activity.self.PersonInfoActivity
import com.sgb.kjwl.view.ui.adapter.self.SelfSecondOrderNestingAdapter
import com.sgb.kjwl.view.widget.self.MenuClickUtils
import com.sgb.kjwl.viewmodel.self.mine.FastMenuViewModel
import com.swgk.core.base.di.AppComponent
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick
import com.swgk.core.util.SharedPreferenceUtil
import javax.inject.Inject

/**
 * 更多快捷键
 */
class FastMenuActivity : AppActivity() {

    private var mData : ArrayList<PermissionMenuEntity.ListEntity> ?= null

    private var mCurrentCompanys: ArrayList<CompanyEntity>? = null

    private var mType : Int = TYPE_COMPANY

    lateinit var mBinding : ActivityFastMenuBinding
    private lateinit var mAdapter : SelfSecondOrderNestingAdapter
    private var mTitle : String ?= null
    @Inject
    lateinit var mViewModel : FastMenuViewModel
    companion object{
        const val KEY_DATA = "key_data"
        const val KEY_TYPE = "key_type"
        const val KEY_TITLE = "key_title"
        const val KEY_COMPANY = "key_company"
        const val TYPE_PERSON = 0
        const val TYPE_COMPANY = 1
        const val TYPE_GROUP = 2
        const val TYPE_ORGANIZATION = 3
        fun launch(context: Context, datas : ArrayList<PermissionMenuEntity.ListEntity>, type : Int, title : String = "快捷菜单"){
            launch(context, datas, type, title, ArrayList())
        }

        fun launch(
            context: Context,
            datas: ArrayList<PermissionMenuEntity.ListEntity>,
            type: Int,
            title: String = "快捷菜单"
           ,mCompanys : ArrayList<CompanyEntity>
        ){
            val intent = Intent(context, FastMenuActivity::class.java)
            intent.putExtra(KEY_DATA, datas)
            intent.putExtra(KEY_TYPE, type)
            intent.putExtra(KEY_TITLE, title)
            intent.putExtra(KEY_COMPANY, mCompanys)
            context.startActivity(intent)
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerSelfComponent.builder().appComponent(appComponent)
            .selfModule(SelfModule()).build().inject(this)
    }

    override fun initView() {
        super.initView()
        if (intent != null){
            mData = intent.getSerializableExtra(KEY_DATA) as ArrayList<PermissionMenuEntity.ListEntity>?
            mType = intent.getIntExtra(KEY_TYPE, TYPE_COMPANY)
            mTitle = intent.getStringExtra(KEY_TITLE)
            mCurrentCompanys = intent.getSerializableExtra(KEY_COMPANY) as ArrayList<CompanyEntity>?
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_fast_menu)
        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.include.titleView.text = mTitle
        initRecyclerView()
    }

    private fun initRecyclerView(){
        mBinding.recycleView.layoutManager = GridLayoutManager(this, 4)
        // SwipeRefreshLayout 刷新
        mBinding.refreshLayout.setEnableLoadMore(false)
        mBinding.refreshLayout.setEnableRefresh(false)
        mBinding.refreshLayout.setOnRefreshListener {
            mBinding.refreshLayout.finishLoadMore(500)
        }
        mAdapter = SelfSecondOrderNestingAdapter(this, mData)
        mBinding.recycleView.adapter = mAdapter
        mAdapter.setOnItemClick(object : AdapterOnItemClick<PermissionMenuEntity.ListEntity>(){
            override fun onItemClick(t: PermissionMenuEntity.ListEntity?, position: Int) {
                if (t != null){
                    handlerClick(t)
                }
            }
        })
    }

    private fun handlerClick(entity : PermissionMenuEntity.ListEntity) {
        if (entity.name == "19_jibenxinxi" || entity.name == "19_jiaruqiye" || entity.name == "26_zhanghuanquan" || entity.name == "18_renzhengzhongxin" || entity.name == "18_erweimamingpian" || entity.name == "18_guanfangkefu" || entity.name == "18_fenxiangapp" || entity.name == "18_yijianfankui" || entity.name == "18_shezhi") {
        } else {
            if (!SharedPreferenceUtil.getInstance()
                    .getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)
            ) {
                ACAdminDialogUtils.showACDialog(ACAdminDialogUtils.TYPE_AUTH_NO_AC, this)
                return
            }
        }
          when(entity.name) {
              "0_qiyezizhi" -> {// 专项资质
                  WebsiteReleaseActivity.start(this, WebsiteReleaseActivity.QIYEZIZHI)
              }
              "0_yejizhengming" -> {// 经营业绩
                  WebsiteReleaseActivity.start(this, WebsiteReleaseActivity.YEJIZHENGMING)
              }
              "0_qiyedongtai" -> {// 经营动态
                  WebsiteReleaseActivity.start(this, WebsiteReleaseActivity.QIYEDONGTAI)
              }
              "0_zhishichanquan" -> {// 知识产权
                  WebsiteReleaseActivity.start(this, WebsiteReleaseActivity.ZHISHICHANQUAN)
              }
              "0_hangyerongyu" -> {// 荣誉荣耀
                  WebsiteReleaseActivity.start(this, WebsiteReleaseActivity.HANGYERONGYU)
              }
              "15_zhaobiaoxuqiu" -> { //招标需求
                  mViewModel.getStringUrl(this, "tenter", "?type=4")
              }
              "15_jingjiaxuqiu" -> {//竞价需求
                  mViewModel.getStringUrl(this, "BidListon", "?type=4");
              }
              "0_wodexinxi" -> {//我的信息
                  PersonInfoActivity.startActivity(this, mCurrentCompanys)
              }
              else -> {
                  MenuClickUtils.getInstance().handlerSelfMenu(this, mType, entity)
              }
          }


    }


}